package com.marvels.system.permission.controller;

import com.marvels.common.controller.BaseController;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.system.permission.dto.SysRoleDTO;
import com.marvels.system.permission.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
/**角色控制器
 * @author 何杰
 * 2018年5月20日
 */
@RestController
@RequestMapping("/system/pmsn/role/service")
public class RoleServiceController  extends BaseController {
	@Autowired
	private SysRoleService sysRoleService;
	 
	@RequestMapping("querySysRoles")
	public MarvelsResultDTO querySysRoles(@RequestBody SysRoleDTO roleDto){
		try {
			MarvelsLogUtil.getInstance().info("----@@@@@@------"+roleDto.getRoleCode());
			List<SysRoleDTO> sysRoles = sysRoleService.getSysRolesByRoleCodes(Arrays.asList(roleDto.getRoleCode().split("_")));
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), sysRoles);
		} catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getOrgTree]",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
	}
	
	@RequestMapping("queryAllSysRoles")
	public MarvelsResultDTO queryAllSysRoles(@RequestBody SysRoleDTO roleDto){
		try {
			MarvelsLogUtil.getInstance().info("----@@@@@@------"+roleDto.getRoleCode());
			List<SysRoleDTO> sysRoles = sysRoleService.getAllSysRolesByRoleCodes(Arrays.asList(roleDto.getRoleCode().split("_")));
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), sysRoles);
		} catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getOrgTree]",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
	}
}
